 package andriod.dao.imp;
 
 import andriod.dao.PersistDao;

import java.io.InputStream;
import java.io.Serializable;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 
 public class PersistDaoImpl extends HibernateDaoSupport
   implements PersistDao
 {
   public List find(String hql)
   {
     return getHibernateTemplate().find(hql);
   }
 
   public List findForPage(final String hql, final int page, final int pagesize)
   {
     return (List)getHibernateTemplate().execute(
       new HibernateCallback()
     {
       public Object doInHibernate(Session session) throws HibernateException, SQLException {
         Query query = session.createQuery(hql);
         query.setFirstResult((page - 1) * pagesize);
         query.setMaxResults(pagesize);
         return query.list();
       }
     });
   }
 
   public long findForLong(final String hql)
   {
     return ((Long)getHibernateTemplate().execute(
       new HibernateCallback()
     {
       public Object doInHibernate(Session session) throws HibernateException, SQLException {
         Query query = session.createQuery(hql);
         return query.uniqueResult();
       }
     })).longValue();
   }
 
   public List<Object[]> query(final String sql)
   {
     return (List<Object[]>)getHibernateTemplate().execute(
       new HibernateCallback()
     {
       public Object doInHibernate(Session session) throws HibernateException, SQLException {
         Query query = session.createSQLQuery(sql);
         return query.list();
       }
     });
   }
 
   public List queryForPage(final String hql, final int page, final int pagesize)
   {
     return (List)getHibernateTemplate().execute(
       new HibernateCallback()
     {
       public Object doInHibernate(Session session) throws HibernateException, SQLException {
         Query query = session.createSQLQuery(hql);
         query.setFirstResult((page - 1) * pagesize);
         query.setMaxResults(pagesize);
         return query.list();
       }
     });
   }
 
   public long queryForLong(final String sql)
   {
     return ((Long)getHibernateTemplate().execute(
       new HibernateCallback()
     {
       public Object doInHibernate(Session session) throws HibernateException, SQLException {
         Query query = session.createSQLQuery(sql);
         return Long.valueOf(((BigDecimal)query.uniqueResult()).longValue());
       }
     })).longValue();
   }
 
   public void executeHql(final String hql)
   {
     getHibernateTemplate().execute(new HibernateCallback()
     {
       public Object doInHibernate(Session session) throws HibernateException, SQLException {
         Query query = session.createQuery(hql);
         return Integer.valueOf(query.executeUpdate());
       }
     });
   }
 
   public void executeSql(final String sql)
   {
     getHibernateTemplate().execute(new HibernateCallback()
     {
       public Object doInHibernate(Session session) throws HibernateException, SQLException {
         Query query = session.createSQLQuery(sql);
         return Integer.valueOf(query.executeUpdate());
       }
     });
   }
 
   public Object getObject(Class entityClass, Integer id)
   {
     return getHibernateTemplate().get(entityClass, id);
   }
 
   public void save(Object obj)
   {
     getHibernateTemplate().save(obj);
   }
 
   public Integer returnSave(Object obj)
   {
     Serializable id = getHibernateTemplate().save(obj);
     return (Integer)id;
   }
 
   public void save(List<Object> list)
   {
     if ((list != null) && (list.size() > 0))
       for (Iterator localIterator = list.iterator(); localIterator.hasNext(); ) { Object obj = localIterator.next();
         save(obj);
       }
   }
 
   public void update(Object obj)
   {
     getHibernateTemplate().update(obj);
   }
 
   public void delete(Object obj)
   {
     getHibernateTemplate().delete(obj);
   }
 
   public void delete(List<Object> list)
   {
     if ((list != null) && (list.size() > 0))
       for (Iterator localIterator = list.iterator(); localIterator.hasNext(); ) { Object obj = localIterator.next();
         delete(obj);
       }
   }
   
   public void updateInfoContent(final String contentId,final byte[] content){
	   getHibernateTemplate().execute(new HibernateCallback() {
		
		@Override
		public Object doInHibernate(Session session) throws HibernateException, SQLException {
			Connection conn = session.connection();
			String sql = "update O_INFO_CONTENT set content=?,file_size=? where id="+contentId;
			PreparedStatement stmt = conn.prepareStatement(sql);
			stmt.setBytes(1, content);
			stmt.setInt(2, content.length);
			stmt.execute();
			return null;
		}
	});
   }

//@Override
//public void updateInfoContent(final String contentId, final InputStream inputStream,final int fileSize) {
//getHibernateTemplate().execute(new HibernateCallback() {
//		
//		@Override
//		public Object doInHibernate(Session session) throws HibernateException, SQLException {
//			Connection conn = session.connection();
//			String sql = "update O_INFO_CONTENT set content=?,file_size=? where id="+contentId;
//			PreparedStatement stmt = conn.prepareStatement(sql);
//			stmt.setBlob(1, inputStream);
//			stmt.setInt(2, fileSize);
//			stmt.execute();
//			return null;
//		}
//	});
//	
//}
   
   
 }